var _$ = function(id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};
function AjaxTip() {
	this.obj = null;
	this.target = null;
	this.tip = null;
	this.url = null;
	this.lists = new Array();
	this.id = AjaxTip.defaultInstanceName;
}

AjaxTip.defaultInstanceName = "lightsoft";

AjaxTip.prototype.setUrl = function(url) {
	this.url = url;
};

AjaxTip.prototype.setTip = function(tip) {
	this.tip = tip;
};
AjaxTip.prototype.setObj = function(obj) {
	this.obj = obj;
};
AjaxTip.prototype.setTarget = function(target) {
	this.target = target;
};
AjaxTip.prototype.init = function() {
	new Form.Element.Observer(this.obj, 1, this.search);
	Event.observe(this.obj, "keydown", this.tipMove, false);
	Event.observe(this.tip, "mousedown", this.tipDown, true);

	// 初始提示层的位置
	var pos = GetObjPos(_$(this.obj))
	_$(this.tip).style.left = pos.x;
	_$(this.tip).style.top = pos.y + 22;

	// 初始客户列表
	this.searchAll();
}
AjaxTip.prototype.showTip = function(originalRequest) {
	if (originalRequest.responseText == "") {
		_$(this.tip).innerHTML = "";
		Element.hide(this.tip);
		return false;
	}
	var brandList = originalRequest.responseText.split("%");
	if (brandList != null && brandList.length > 0) {
		var bt = _$(this.tip);
		var s = "";
		var flog = 0;
		for ( var i = 0; i < brandList.length; i++) {
			if (flog == 30) {
				break;
			}
			var curBrand = brandList[i].split("$");
			this.lists[curBrand[1]] = curBrand[0];
			s += "<div onmouseover=\"this.className='selectTip';style.cursor='default'\"  onmouseout=\"this.className=null; style.cursor='default'\">"
					+ curBrand[1] + "</div>";
			flog++;
		}
		bt.innerHTML = s;
		if (tips != this.obj.value) {
			var pos = GetObjPos(this.obj)
			_$(this.tip).style.left = pos.x;
			_$(this.tip).style.top = pos.y + 22;
			Element.show(this.tip);
		}
	} else {
		var bt = _$(this.tip);
		bt.innerHTML = "";
		Element.hide(this.tip);
	}
};
AjaxTip.prototype.search = function() {
	if (this.url == null) {
		return;
	}
	var params = "name=" + _$(this.obj).value;
	var myAjax = new Ajax.Request(this.url, {
		method : 'post',
		parameters : params,
		onComplete : this.showTip,
		asynchronous : true
	});
};
AjaxTip.prototype.searchAll = function() {
	if (this.url == null) {
		return;
	}
	var myAjax = new Ajax.Request(this.url, {
		method : 'post',
		parameters : "",
		onComplete : this.showAll,
		asynchronous : true
	});
};
AjaxTip.prototype.showAll = function(originalRequest) {
	if (originalRequest.responseText.trim() == "") {
		return false;
	}
	var brandList = originalRequest.responseText.split("%");
	if (brandList != null && brandList.length > 0) {
		for ( var i = 0; i < brandList.length; i++) {
			var curBrand = brandList[i].split("$");
			this.lists[curBrand[1].trim()] = curBrand[0].trim();
		}
	}
}
AjaxTip.prototype.tipMove = function(event) {
	var srcEl = Event.element(event);
	var tipEl = _$(this.tip);
	var a = tipEl.childNodes;
	if (tipEl.style.display == "") {
		if (event.keyCode == 40) {
			if (tipEl.childNodes.length >= 1) {
				var bList = tipEl.childNodes;

				if (tipEl.lastChild.className == "selectTip") {
					tipEl.firstChild.className = "selectTip";
					tipEl.lastChild.className = "null";
					return;
				}
				var s = 0;
				for ( var i = 0; i < bList.length; i++) {
					if (bList[i].className == "selectTip") {
						s++;
						bList[i + 1].className = "selectTip";
						bList[i].className = "null";
						return;
					}
				}
				if (s == 0) {
					tipEl.firstChild.className = "selectTip";
				}
			}
		} else if (event.keyCode == 38) {
			if (tipEl.childNodes.length >= 1) {
				if (tipEl.firstChild.className == "selectTip") {
					tipEl.lastChild.className = "selectTip";
					tipEl.firstChild.className = "null";
					return;
				}
				var s = 0;
				var bList = tipEl.childNodes;
				for ( var i = 0; i < bList.length; i++) {
					if (bList[i].className == "selectTip") {
						s++;
						bList[i - 1].className = "selectTip";
						bList[i].className = "null";
						return;
					}
				}
				if (s == 0) {
					tipEl.lastChild.className = "selectTip";
				}
			}
		} else if (event.keyCode == 13) {
			var bList = tipEl.childNodes;
			for ( var i = 0; i < bList.length; i++) {
				if (bList[i].className == "selectTip") {
					tips = srcEl.value = bList[i].innerHTML;
					Element.hide(tipEl);
					return;
				}
			}
		}
	}
}
AjaxTip.prototype.tipDown = function(event) {
	var srcEl = Event.element(event);
	var tipEl = _$(this.tip);
	;
	var bList = tipEl.childNodes;

	for ( var i = 0; i < bList.length; i++) {
		if (bList[i].className == "selectTip") {
			tips = srcEl.value = bList[i].innerHTML;
			_$(this.obj).value = bList[i].innerHTML;
			Element.hide(tipEl);
			return;
		}
	}
}
AjaxTip.prototype.setValue = function() {
	if (_$(this.obj).value != "") {
		var brand = _$(this.obj).value;
		brand = brand.trim();
		if (brand in this.lists) {
			_$(this.target).value = this.lists[brand];
		} else { 
			_$(this.obj).value = "";
			_$(this.target).value = "";
			_$(this.obj).focus();
		}
	}

	if (_$(this.obj).value.length == 0) {
		_$(this.target).value = "";
	}

	Element.hide(this.tip);
}
AjaxTip.prototype.isAsynch = function() {
	return document.cookie.indexOf(AjaxAnywhere.SYNCH_KEY) == -1;
};

AjaxTip.prototype.bindById = function() {
	var key = "Lightsoft." + this.id;
	window[key] = this;
}

AjaxTip.findInstance = function(id) {
	var key = "Lightsoft." + id;
	return window[key];
}

ajaxTip = new AjaxTip();
ajaxTip.bindById();

